#/usr/bin/bash
# 备份系统默认源
mkdir /etc/yum.repos.d/old && mv /etc/yum.repos.d/C* /etc/yum.repos.d/old/
yum clean all
# 设置阿里yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# pip源(2.8.4的bug)
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
trusted-host=mirrors.aliyun.com
index-url=https://mirrors.aliyun.com/pypi/simple/
EOF
#配置 SSH 
sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config
systemctl restart sshd
#禁用 SElinux
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
setenforce 0
#配置对应防火墙规则

# TFTP
firewall-cmd --zone=public --add-port=69/tcp --permanent
firewall-cmd --zone=public --add-port=69/udp --permanent
# HTTPD
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
# Cobbler
firewall-cmd --zone=public --add-port=25150/tcp --permanent
firewall-cmd --zone=public --add-port=25150/udp --permanent
# Koan
firewall-cmd --zone=public --add-port=25151/tcp --permanent
# Samba Window  安装需要用到 Samba,否则可以不开放
firewall-cmd --zone=public --add-port=139/tcp --permanent
firewall-cmd --zone=public --add-port=445/tcp --permanent
firewall-cmd --zone=public --add-port=137/udp --permanent
firewall-cmd --zone=public --add-port=138/udp --permanent
# 更新防火墙规则
firewall-cmd --reload

#清除因网络安装系统时创建的repo
rm -fr /etc/yum.repos.d/cobbler-config.repo
#安装cobbler及依赖软件
yum install -y cobbler cobbler-web dhcp tftp-server pykickstart httpd xinetd git fence-agents debmirror
#设置开机启动
systemctl enable httpd&&systemctl enable xinetd&&systemctl enable rsyncd&&systemctl enable tftp&&systemctl enable tftp
# systemctl enable xinetd
# systemctl enable rsyncd
# systemctl enable tftp
# systemctl enable tftp
#启动服务
systemctl start httpd&&systemctl start xinetd&&systemctl start tftp&&systemctl start cobblerd 
# systemctl start httpd
# systemctl start xinetd
# systemctl start tftp
# systemctl start cobblerd



#设置服务器ip地址。  
export cobbler_ip=`ip address|grep "/24"|awk '{print$2}'|cut -d / -f1`
sed -i "s%^server: 127.0.0.1%server: ${cobbler_ip}%g" /etc/cobbler/settings
sed -i "s%^next_server: 127.0.0.1%next_server: ${cobbler_ip}%g" /etc/cobbler/settings

#开启tftp功能
sed -i '/disable\>/s/\<yes\>/no/' /etc/xinetd.d/tftp

#复制文件到/var/lib/cobbler/loaders/目录下。
git clone https://gitee.com/red_peg/cobbler_script.git
cp ./cobbler_script/* /var/lib/cobbler/loaders

#下载bootload
cobbler get-loaders

#下载 Ubuntu本地包镜像
# yum install -y debmirror
sed -i 's%^@dists="sid"%#@dists="sid"%g;s%@arches="i386"%#@arches="i386"%g' /etc/debmirror.conf
#设置安装系统后的root密码
root_pwd=$(openssl passwd -1 -salt `openssl rand 15 -base64` 'yourpasswd')
sed -i "s%^default_password_crypted.*%default_password_crypted: \"${root_pwd}\"%g" /etc/cobbler/settings
#安装电源管理模块（非必需）
# yum -y install fence-agents

#修改配置文件cobbler接管dhcp
sed -i "s%manage_dhcp: 0%manage_dhcp: 1%g" /etc/cobbler/settings
sed -i "s%pxe_just_once: 0%pxe_just_once: 1%g" /etc/cobbler/settings

#配置dhcp服务:/etc/cobbler/dhcp.template 网段设置为自己主机网段
#得到ip_route 地址
export ip_route=`ip route | grep "via"| awk '{print$3}'`
#得到本机网段
export ip_network=`echo "${ip_route}"|cut -d . -f1-3`

#sed需要双引号才支持变量
sed -i "s/192.168.1/${ip_network}/g"      /etc/cobbler/dhcp.template
sed -ri "s/${ip_network}.[1,5];/${ip_route};/g"      /etc/cobbler/dhcp.template


#配置web用户名密码用户名：amdin 密码:admin123
htdigest -c /etc/cobbler/users.digest admin123 admin


#+++++++++++++++++++++++++执行下面代码前先将镜像复制到/tmp目录下面+++++++++++++++++++++++++++++++
#+++++++++++++++++++++++可用Winscp或Mobaxterm或filezilla等工具++++++++++++++++++++++++++++++++++
#镜像挂载 挂载前先将镜像放在\tmp\目录下。
###############################################centos 7 部分设置#################################################################
#centos镜像名称CentOS-7-x86_64-Minimal-1908
export centos_image_name=CentOS-7-x86_64-Minimal-1908

mount -t iso9660 -o loop /tmp/${centos_image_name}.iso /mnt/
cobbler import --name=${centos_image_name} --path=/mnt/ --arch=x86_64
#获取要安装的centso profile名字。
export centos_profile_name="`ls /var/www/cobbler/images | grep '1908'`"
cobbler profile edit --name=${centos_profile_name} --kopts='net.ifnames=0 biosdevname=0'
#配置kickstarts进行自动安装  centos的ks文件
cp /var/lib/cobbler/kickstarts/sample_end.ks /var/lib/cobbler/kickstarts/${centos_profile_name}.ks
cobbler profile edit --name=${centos_profile_name}  --kickstart=/var/lib/cobbler/kickstarts/${centos_profile_name}.ks
###########################################ubuntu部分################################################################################
#ubuntu镜像名称
export ubuntu_image_name=ubuntu-16.04.6-server-amd64
mount -t iso9660 -o loop /tmp/${ubuntu_image_name}.iso /mnt/
cobbler import --name=${ubuntu_image_name} --path=/mnt/ --arch=x86_64

#配置kickstarts进行自动安装   #ubuntu的ks文件。 注：ubuntu 安装时要断开网络否则安装非常慢，因为更新慢的要命。
export ubuntu_profile_name="`ls /var/www/cobbler/images | grep "hwe"`"
cp /var/lib/cobbler/kickstarts/sample.seed   /var/lib/cobbler/kickstarts/${ubuntu_profile_name}.seed
cobbler profile edit --name=${ubuntu_profile_name}  --kickstart=/var/lib/cobbler/kickstarts/${ubuntu_profile_name}.seed
##########################################windows 10部分##################################################################################
##下载ADK参考地址：https://www.cnblogs.com/wenzhongxiang/p/10673124.html
#下载 Windows 10 ADK version 1809	10.1.17763.1	Link https://go.microsoft.com/fwlink/?linkid=2026036
#下载：windows 10 下载工具进行下载最新版系统：https://www.microsoft.com/zh-cn/software-download/windows10
#下载PE:http://download.microsoft.com/download/D/7/E/D7E22261-D0B3-4ED6-8151-5E002C7F823D/adkwinpeaddons/adkwinpesetup.exe
#安装下载的软件:
#制作PE系统
#注意，ADK 的两个都要下载，这俩都是引导包，真正的安装程序会由这俩软件进行下载。其中 WinPE 需要用到 5G 左右的磁盘空间.
#安装完后，以管理员身份打开部署和映像工具环境。
#++++++++++++以下代码为在windows10平台定制windows10PE
#1、本地生成 Winpe 文件目录
copype amd64 C:\winpe
#2、Dism 挂载 Winpe 的启动文件到 Winpe的 Mount 目录。
Dism /mount-image /imagefile:C:\winpe\media\sources\boot.wim /index:1 /mountdir:C:\winpe\mount
#3、将启动命令硬编码写死到 Winpe 的 startnet.cmd 文件里。
echo net use z: \\192.168.0.253\share >> C:\winpe\mount\Windows\System32\startnet.cmd
#4、无人值守安装。
echo z:\win\setup.exe /unattend:z:\win\win10_x64_bios_auto.xml >> C:\winpe\mount\Windows\System32\startnet.cmd
#5、卸载 Winpe 的挂载（一定要执行，否则直接强制删除文件夹会出一些稀奇古怪的问题）。
Dism /unmount-image /mountdir:C:\winpe\mount /commit
#6、制作 Win10 镜像，名为 winpe_win10_amd64.iso。
MakeWinPEMedia /ISO C:\winpe C:\winpe\winpe_win10_amd64.iso
#++++++++++++++++++以下代码在linux服务器上执行+++++++++
#使用filezilla等工具将winpe_win10_amd64.iso上传到cobbler服务器/tmp/目录下.
#增加windows10 distro 文件.
cobbler distro add --name=windows_10_x64 --kernel=/var/lib/tftpboot/memdisk --initrd=/root/winpe_win10_amd64.iso --kopts="raw iso"
#创建空应答文件winpe.xml。
touch /var/lib/cobbler/kickstarts/winpe.xml
#增加windows10 profile 文件.
cobbler profile add --name=windows_10_x64 --distro=windows_10_x64 --kickstart=/var/lib/cobbler/kickstarts/winpe.xml
#但是如果使用直接生成的，有点问题，即使页面设置了安装语言，但是仍旧需要手动选择，经过多方研究发现主要卡在 UILanguage 和 Inputlocale 上，全写 zh-CN 无效。
#+++++++++++++++++++++++++++设置samba服务+++++++++++++++++++++++
#安装samba
yum install samba -y
#修改smb配置文件
vim /etc/samba/smb.conf
[global]
log file = /var/log/samba/log.%m
max log size = 5000
security = user
guest account = nobody
map to guest = Bad User
load printers = yes
cups options = raw
 
[share]
comment = share directory目录
path = /smb/
directory mask = 0755
create mask = 0755
guest ok=yes
writable=yes
#+++++++++++++启动smb服务+++++++++++++
service smb start
systemctl enable smb
#+++++++++++++挂载windows10系统+++++++
#创建服务器共享目录/smb/win
mkdir -p /smb/win
#修改共享目录权限为0755.
chmod -R 0755 /smb/win
#将系统挂载到/mnt/目录下面
mount -o loop,ro /tmp/your_system_name.iso /mnt/
#将系统复制到smb服务器共享目录/smb/win下面
cp -r /mnt/* /smb/win
#取消挂载
umount /mnt/
#++++++++++++++++++++以上配置只能用在legcay模式启动因为memdisk不支持uefi格式引导。
#若想用uefi引导需要将winpe_win10_amd64.iso以UEFI格式刻录到U盘上或采用光盘进行引导。
#同时自动应答文件要改成UEFI应答文件。
##########################################################################################################################################
#执行同步cobbler sync
cobbler sync

#重启相关服务
systemctl restart httpd&&systemctl restart xinetd&&systemctl restart tftp&&systemctl restart cobblerd
# systemctl restart httpd
# systemctl restart xinetd
# systemctl restart tftp
# systemctl restart cobblerd

########################################################################################################
######################              注意                 ################################################
######################配置环境CentOS-7-x86_64-Minimal-1908############################################### 
######################镜像名称需要输入即：export centos_image_name的值##################################### 
######################web账号密码:Cobbler  cobbler######################################################
#####################安装完成后客户机密码为:yourpasswd###################################################
#####################ubuntu安装时要断开网络否则非常慢，原因：联网更新。######################################
#####################Centos windows10安装时要2GB的内存否则安装会失败；运行时内存最小为256mb#################################
#参考：https://mp.weixin.qq.com/s?__biz=MzI3MTI2NzkxMA==&mid=2247486676&idx=1&sn=b500c18f7e72f501455cd8824cc79a0b&chksm=eac52dfdddb2a4eb7350e98b35a492e05c2ff8fd0a6b9127d59e9f5e977604c8354263d32329&scene=21#wechat_redirect
#https://blog.csdn.net/weixin_44504261/article/details/100166146
#https://www.kancloud.cn/devops-centos/centos-linux-devops
#https://cobbler.readthedocs.io/en/latest/quickstart-guide.html
#https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/installation_guide/sect-kickstart-syntax
